home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Source / GNU / libg++ / libiberty / concat.c < prev    next >
C/C++ Source or Header  |  1994-02-15  |  3KB  |  119 lines

  1. /* Concatenate variable number of strings.
  2.    Copyright (C) 1991 Free Software Foundation, Inc.
  3.    Written by Fred Fish @ Cygnus Support
  4.  
  5. This file is part of the libiberty library.
  6. Libiberty is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public
  8. License as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10.  
  11. Libiberty is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. Library General Public License for more details.
  15.  
  16. You should have received a copy of the GNU Library General Public
  17. License along with libiberty; see the file COPYING.LIB.  If
  18. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  19. Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /*
  23.  
  24. NAME
  25.  
  26.     concat -- concatenate a variable number of strings
  27.  
  28. SYNOPSIS
  29.  
  30.     #include <varargs.h>
  31.  
  32.     char *concat (s1, s2, s3, ..., NULL)
  33.  
  34. DESCRIPTION
  35.  
  36.     Concatenate a variable number of strings and return the result
  37.     in freshly malloc'd memory.
  38.  
  39.     Returns NULL if insufficient memory is available.  The argument
  40.     list is terminated by the first NULL pointer encountered.  Pointers
  41.     to empty strings are ignored.
  42.  
  43. NOTES
  44.  
  45.     This function uses xmalloc() which is expected to be a front end
  46.     function to malloc() that deals with low memory situations.  In
  47.     typical use, if malloc() returns NULL then xmalloc() diverts to an
  48.     error handler routine which never returns, and thus xmalloc will
  49.     never return a NULL pointer.  If the client application wishes to
  50.     deal with low memory situations itself, it should supply an xmalloc
  51.     that just directly invokes malloc and blindly returns whatever
  52.     malloc returns.
  53. */
  54.  
  55.  
  56. #include <varargs.h>
  57.  
  58. #define NULLP (char *)0
  59.  
  60. extern char *xmalloc ();
  61.  
  62. /* VARARGS */
  63. char *
  64. concat (va_alist)
  65.      va_dcl
  66. {
  67.   register int length = 0;
  68.   register char *newstr;
  69.   register char *end;
  70.   register char *arg;
  71.   va_list args;
  72.  
  73.   /* First compute the size of the result and get sufficient memory. */
  74.  
  75.   va_start (args);
  76.   while ((arg = va_arg (args, char *)) != NULLP)
  77.     {
  78.       length += strlen (arg);
  79.     }
  80.   newstr = (char *) xmalloc (length + 1);
  81.   va_end (args);
  82.  
  83.   /* Now copy the individual pieces to the result string. */
  84.  
  85.   if (newstr != NULLP)
  86.     {
  87.       va_start (args);
  88.       end = newstr;
  89.       while ((arg = va_arg (args, char *)) != NULLP)
  90.     {
  91.       while (*arg)
  92.         {
  93.           *end++ = *arg++;
  94.         }
  95.     }
  96.       *end = '\000';
  97.       va_end (args);
  98.     }
  99.  
  100.   return (newstr);
  101. }
  102.  
  103. #ifdef MAIN
  104.  
  105. /* Simple little test driver. */
  106.  
  107. main ()
  108. {
  109.   printf ("\"\" = \"%s\"\n", concat (NULLP));
  110.   printf ("\"a\" = \"%s\"\n", concat ("a", NULLP));
  111.   printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP));
  112.   printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP));
  113.   printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP));
  114.   printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP));
  115.   printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP));
  116. }
  117.  
  118. #endif
  119.